package C7_1_ComposedMethod.after;

/**
 * @author xufy
 * @version 1.0
 **/
public class ListDemo {

    private final static int GROWTH_INCREMENT=10;

    boolean readOnly;
    int size = 3;
    Object[] elements = {1, 2, 3};

    public void add(Object element) {
        if (readOnly) {
            return;
        }
        if (atCapacity()) {
           grow();
        }
        addElement(element);
    }
    private void addElement(Object element){
        elements[size++] = element;
    }
    private void grow(){
        Object[] newElements = new Object[elements.length + GROWTH_INCREMENT];
        for (int i = 0; i < size; i++) {
            newElements[i] = elements[i];
        }
        elements = newElements;
    }
    private boolean atCapacity(){
        return (size+1) > elements.length;

    }


}
